package com.ruoyi.a2.service.impl;

import com.ruoyi.a2.domain.A2DynamicsInfo;
import com.ruoyi.a2.domain.A2HPMConfig;
import com.ruoyi.a2.domain.A2Pdcc;
import com.ruoyi.a2.domain.A2StaticInfo;
import com.ruoyi.a2.domain.A2TargetPabc;
import com.ruoyi.a2.domain.A2TmpAndIa;
import com.ruoyi.a2.dto.A2DynamicsInfoCondition;
import com.ruoyi.a2.dto.A2HPMConfigCondition;
import com.ruoyi.a2.dto.A2HPMConfigForm;
import com.ruoyi.a2.dto.A2HPMConfigJson;
import com.ruoyi.a2.dto.A2PdccCondition;
import com.ruoyi.a2.dto.A2PdccJson;
import com.ruoyi.a2.dto.A2StaticInfoForm;
import com.ruoyi.a2.dto.A2TmpAndIaJson;
import com.ruoyi.a2.dto.TargetPabcJson;
import com.ruoyi.a2.dto.A2StaticInfoCondition;
import com.ruoyi.a2.dto.TargetActivityExtractionJson;
import com.ruoyi.a2.dto.TargetPabcCondition;
import com.ruoyi.a2.service.IA2DynamicsInfoService;
import com.ruoyi.a2.service.IA2HPMConfigService;
import com.ruoyi.a2.service.IA2PdccService;
import com.ruoyi.a2.service.IA2Service;
import com.ruoyi.a2.service.IA2StaticInfoService;
import com.ruoyi.a2.service.IA2TargetPabcService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.CommonOKNOEnum;
import com.ruoyi.common.enums.FlightPathTypeEnum;
import com.ruoyi.common.mapper.CommonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;


@Slf4j
@Service
public class A2ServiceImpl implements IA2Service {

    @Autowired
    private IA2StaticInfoService a2StaticInfoService;

    @Autowired
    private IA2DynamicsInfoService a2DynamicsInfoService;

    @Autowired
    private IA2PdccService a2PdccService;

    @Autowired
    private IA2TargetPabcService a2TargetPabcService;

    @Autowired
    private IA2HPMConfigService a2HPMConfigService;

    @Autowired
    private CommonMapper commonMapper;

    @Override
    public TableDataInfo a2StaticInfoList(A2StaticInfoCondition condition) {
        return a2StaticInfoService.page(condition);
    }

    @Override
    public TableDataInfo a2DynamicsInfoList(A2DynamicsInfoCondition condition) {
        return a2DynamicsInfoService.page(condition);
    }

    @Override
    public List<A2DynamicsInfo> a2DynamicsInfoHistory(A2DynamicsInfoCondition condition) {
        return a2DynamicsInfoService.a2DynamicsInfoHistory(condition);
    }

    @Override
    public A2Pdcc getA2Pdcc(A2PdccCondition condition) {
        A2Pdcc a2Pdcc = new A2Pdcc();
        a2Pdcc.setType(condition.getType());
        List<A2Pdcc> list = a2PdccService.selectA2PdccList(a2Pdcc);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateA2Pdcc(A2PdccJson json) {
        List<A2Pdcc> list = json.getList();
        for (A2Pdcc bean : list) {
            a2PdccService.updateA2Pdcc(bean);
        }
    }

    @Override
    public void startStopTAE(TargetActivityExtractionJson json) {
        a2StaticInfoService.updateEnableStatus(json);
    }

    @Override
    public A2TargetPabc getTargetPabc(TargetPabcCondition condition) {
        A2TargetPabc a2TargetPabc = new A2TargetPabc();
        a2TargetPabc.setAbnormalBehavior(condition.getType());
        List<A2TargetPabc> list = a2TargetPabcService.selectA2TargetPabcList(a2TargetPabc);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateTargetPabc(TargetPabcJson json) {
        List<A2TargetPabc> list = json.getList();
        for (A2TargetPabc bean : list) {
            a2TargetPabcService.updateA2TargetPabc(bean);
        }
    }

    @Override
    public int removeA2StaticInfo(Long[] ids) {
        return a2StaticInfoService.deleteA2StaticInfoByIds(ids);
    }

    @Override
    public A2StaticInfo getA2StaticInfo(Long id) {
        return a2StaticInfoService.selectA2StaticInfoById(id);
    }

    @Override
    public AjaxResult saveOrUpdateA2StaticInfo(A2StaticInfoForm form) {
        return a2StaticInfoService.saveOrUpdate(form);
    }

    @Override
    public TableDataInfo a2HPMConfigList(A2HPMConfigCondition condition) {
        return a2HPMConfigService.page(condition);
    }

    @Override
    public A2HPMConfig getA2HPMConfig(Long id) {
        return a2HPMConfigService.selectA2HPMConfigById(id);
    }

    @Override
    public int removeA2HPMConfig(Long[] ids) {
        return a2HPMConfigService.deleteA2HPMConfigByIds(ids);
    }

    @Override
    public AjaxResult saveOrUpdateA2HPMConfig(A2HPMConfigForm form) {
        return a2HPMConfigService.saveOrUpdate(form);
    }

    @Override
    public void startStopA2HPMConfig(A2HPMConfigJson json) {
        a2HPMConfigService.updateEnableStatus(json);
    }

    @Override
    public List<A2TmpAndIa> a2TmpAndIa(A2TmpAndIaJson json) {
        FlightPathTypeEnum flightPathTypeEnum = FlightPathTypeEnum.getByCode(json.getType());
        //动态组装sql，查询AIS/ADS-B动态信息表
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ")
                .append("lon,lat,base_time")
                .append(" FROM ")
                .append(flightPathTypeEnum.getDynamicsTableName())
                .append(" WHERE ")
                .append(flightPathTypeEnum.getMmsiIcao()).append(" ='").append(json.getTargetNum()).append("'")
                .append(" AND base_time >='").append(json.getStartTime()).append("'")
                .append(" AND base_time <='").append(json.getEndTime()).append("'")
                .append(" ORDER BY base_time");
        //List<LinkedHashMap<String, Object>> linkedHashMaps = commonMapper.selectListBySql(sql.toString());
        return null;
    }

}
